

package cn.shengchao.examstar.dict.acl.adapters.repositories;

import cn.shengchao.examstar.dict.acl.ports.repositories.IExamStarDictRepository;
import cn.shengchao.examstar.dict.infrastructure.dao.DictDao;
import cn.shengchao.examstar.dict.infrastructure.dao.DictItemDao;
import cn.shengchao.examstar.dict.acl.ports.message.DictDto;
import cn.shengchao.examstar.dict.infrastructure.dao.po.TDict;
import cn.shengchao.examstar.dict.infrastructure.dao.po.TDictItem;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class ExamStarDictRepository implements IExamStarDictRepository {

    @Autowired
    private DictDao dictDao;

    @Autowired
    private DictItemDao dictItemDao;

    @Override
    public List<DictDto> getList(String code, Long pid) {
        TDict dict = dictDao.selectOne(new LambdaQueryWrapper<TDict>().eq(TDict::getCode, code));
        if (dict == null) {
            return new ArrayList<>();
        }
        return dictItemDao.selectList(new LambdaQueryWrapper<TDictItem>().eq(TDictItem::getDictId, dict.getId())
            .eq(pid != null, TDictItem::getPId, pid)
            .orderByAsc(TDictItem::getSort)).stream().map(item -> {
                DictDto dto = new DictDto();
                dto.setId(String.valueOf(item.getId()));
                dto.setName(item.getLabel());
                dto.setDescription(item.getDescription());
                return dto;
            }).collect(Collectors.toList());
    }

    @Override
    public List<DictDto> getAllList() {
        return dictDao.selectList(new LambdaQueryWrapper<>()).stream().map(item -> {
            DictDto dto = new DictDto();
            dto.setId(String.valueOf(item.getId()));
            dto.setName(item.getName());
            dto.setDescription(item.getCode());
            return dto;
        }).toList();
    }

    @Override
    public List<DictDto> getAllItemList() {
        return dictItemDao.selectList(new LambdaQueryWrapper<>()).stream().map(item -> {
            DictDto dto = new DictDto();
            dto.setId(String.valueOf(item.getId()));
            dto.setName(item.getLabel());
            dto.setDescription(item.getDescription());
            return dto;
        }).collect(Collectors.toList());
    }
}
